package com.bawei.module_home.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_home.databinding.HourseItemBinding
import com.bawei.module_home.model.HomeServiceModel
import com.bawei.module_home.model.HouseItem


class HouseAdapter(): RecyclerView.Adapter<HouseAdapter.HouseViewHolder>() {
    val houseList= mutableListOf<HouseItem>()
    // 定义点击回调接口
    interface OnItemClickListener {
        fun onItemClick(item: HouseItem, position: Int)
    }
    var itemClickListener: OnItemClickListener? = null
    // 设置点击监听器
    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.itemClickListener = listener
    }




    val mAdapter2:HouseAdapter2 by lazy {
        HouseAdapter2()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HouseViewHolder {
       val binding=HourseItemBinding.inflate(
          LayoutInflater.from(parent.context)
           ,parent,
           false
       )

        return HouseViewHolder(binding)

    }

    override fun getItemCount(): Int {
        return houseList.size
    }
    fun setData(data:List<HouseItem>){
        houseList.clear()
        houseList.addAll(data)
        notifyDataSetChanged()
    }


    override fun onBindViewHolder(holder: HouseViewHolder, position: Int) {
        val binding=DataBindingUtil.getBinding<HourseItemBinding>(holder.itemView)
        binding?.houseNumber?.setText(houseList[position].enterpriseId.toString()+"栋")
        binding?.houseArea?.setText(houseList[position].area.toString()+"m2")
        binding?.houseExpand?.setOnClickListener {
            if (binding.houseInfo.visibility== View.GONE){
                binding.houseInfo.visibility= View.VISIBLE

                binding.rvHouse2.adapter= mAdapter2
                binding.rvHouse2.layoutManager= GridLayoutManager(holder.itemView.context,2)

                // 点击事件
                itemClickListener?.onItemClick(houseList[position], position)


            }else{
                binding.houseInfo.visibility= View.GONE
            }
        }


    }


    class HouseViewHolder(item:HourseItemBinding):RecyclerView.ViewHolder(item.root){}

}